package com.qingke.product.Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.qingke.product.dao.ProductDao;
import com.qingke.product.pojo.Goods;

@WebServlet("/List")
public class ListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ListServlet() {
        super();
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		Integer id=request.getParameter("id")==null||request.getParameter("id").equals("")?null:Integer.valueOf(request.getParameter("id"));
		
		ProductDao pd=new ProductDao();
		Gson gson=new Gson();
		if(id!=null){
			Goods goods=pd.getGoodsById(id);
			String result=gson.toJson(goods);
			response.getWriter().write(result);
			pd.close();
			return ;
		}
		List<Goods> list =pd.getAllGoods();
		String result=gson.toJson(list);
		response.getWriter().write(result);
		pd.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
